<template>
	<view class="search-container">
		<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResult.length !== 0">
      <view class="sugg-item" v-for="(item,i) in searchResult" :key=i @click="gotoDetail(item.goods_id)">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type='arrowright' size="16"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史 -->
    <view class="search-history" v-else>
      <!-- 标题区域 -->
      <view class="search-title">
        <text>搜索历史</text>
        <uni-icons type='trash' size='17' @click='cleanHistory'></uni-icons>
      </view>
        <!-- 列表区域 -->
        <view class="search-list">
          <uni-tag :text="item" v-for="(item,i) in histories" :key='i'></uni-tag>
        </view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:null,
        kw:'',
        searchResult:[],
        historyList:[]
			};
		},
    
    onLoad() {
      this.historyList = uni.getStorageSync(JSON.parse('kw') || '[]')
    },
    
    computed:{
      histories(){
        return [...this.historyList].reverse()
      }
    },
    
    methods:{
      input(e){
        clearTimeout(this.timer)
        // 设置一个定时器把他的值赋值给timer
        this.timer = setTimeout(()=>{
          this.kw = e.value
          // console.log(this.kw)
          this.getResult()
        },500)
      },
      
      // 根据关键字搜索列表
      async getResult(){
        // 判断关键字是否为空
        if(this.kw === ''){
          this.searchResult = []
          return
        }
        this.saveHistory()
        const { data : res} = await uni.$http.get('/goods/qsearch',{query:this.kw})
        if(res.meta.status !== 200) return uni.$showMsg()
        this.searchResult = res.message
      },
      
      // 跳转到商品详情
      gotoDetail(goods_id){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?good_id=' + goods_id
        })
      },
      
      // 定义一个saveHistory保存关键词的历史记录
      saveHistory(){
        // this.historyList.push(this.kw)
        // 创建一个新的set对象，将数组转化为新的set对象
        const set = new Set(this.historyList)
        // 删除对象中的元素
        set.delete(this.kw)
        // 向对象中添加元素
        set.add(this.kw)
        // 将set对象转化成为数组
        this.historyList = Array.from(set)
        // 将历史记录持久化存储到本地
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      
      cleanHistory(){
        this.historyList = []
        uni.setStorageSync('kw','[]')
      }
    }
	}
</script>

<style lang="scss">
.search-container{
  position: sticky;
  top: 0;
  z-index: 999;
  
  .search-history{
    .search-title{
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding:0 15px;
      height: 40px;
      border-bottom: 1px solid #c00000;
    }
    
    .search-list{
      display: flex;
      flex-wrap: wrap;
      
      .uni-tag{
        margin: 5px 5px 0 0;
      }
    }
  }
  
  .sugg-list{
    padding: 0 5px;
    
    .sugg-item{
   display: flex;
   align-items: center;
   justify-content: space-between;
   border-bottom: 1px solid #c00000;
   height: 60rpx;
   font-size: 14px;
    }
    .goods-name{
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-right: 3px;
    }
    
   
    }
}


</style>
